package org.springblade.modules.ad.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springblade.modules.ad.entity.AdBase;
import org.springblade.modules.ad.entity.AdGroup;
import org.springblade.modules.ad.entity.AdOU;
import org.springblade.modules.ad.entity.AdUser;
import org.springblade.modules.ad.service.LdapService;
import org.springblade.modules.ad.utils.LdapUtil;
import org.springblade.modules.ad.utils.Result;
import org.springframework.ldap.core.AttributesMapper;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.naming.Name;
import java.util.List;

import static org.springblade.modules.ad.utils.LdapUtil.buildDn;
import static org.springframework.ldap.query.LdapQueryBuilder.query;

@Service
public class LdapServiceImpl implements LdapService {
	private final Logger logger = LoggerFactory.getLogger(getClass());


	@Resource
	private LdapTemplate ldapTemplate;

	@Override
	public Result renameDn(AdBase adBase) {
		try {
			Name oldName = buildDn(adBase.getBasePath(), adBase.getOldDN(), adBase.getAdDNType());
			Name newName = buildDn(adBase.getBasePath(), adBase.getNewDN(), adBase.getAdDNType());
			ldapTemplate.rename(oldName, newName);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return Result.of().success();
	}

	@Override
	public Result deleteDN(AdBase adBase) {
		Name name = buildDn(adBase.getBasePath(), adBase.getOldDN(), adBase.getAdDNType());
		ldapTemplate.unbind(name);
		return Result.of().success();
	}

	@Override
	public List getAll(AdBase adBase) {
		Name baseDn = buildDn(adBase.getBasePath(), null, null);
		// OU
		if (adBase.getObjectClassType().equals("organizationalUnit")) {
			List<AdOU> personList = ldapTemplate.search(query().base(baseDn).where("objectClass").is(adBase.getObjectClassType()),
				(AttributesMapper) attributes -> LdapUtil.objectToContextAttribute(attributes, new AdOU()));
			personList.stream().map(ou -> "getAll" + adBase.getObjectClassType() + ou.getName() + "------" + ou.getDescription()).forEach(logger::info);
			return personList;
		}
		// 域
		else if (adBase.getObjectClassType().equals("group")) {
			List<AdGroup> personList = ldapTemplate.search(query().base(baseDn).where("objectClass").is(adBase.getObjectClassType()),
				(AttributesMapper) attributes -> LdapUtil.objectToContextAttribute(attributes, new AdGroup()));
			personList.stream().map(group -> "getAll" + adBase.getObjectClassType() + group.getName() + "------" + group.getDescription()).forEach(logger::info);
			return personList;
		}
		// 用户
		else {
			List<AdUser> personList = ldapTemplate.search(query().base(baseDn).where("objectClass").is(adBase.getObjectClassType()),
				(AttributesMapper) attributes -> LdapUtil.objectToContextAttribute(attributes, new AdUser()));
			personList.stream().map(person -> "getAll" + adBase.getObjectClassType() + person.getGivenName() + "------" + person.getDescription()).forEach(logger::info);
			return personList;
		}
	}



}
